import { type NextRequest, NextResponse } from "next/server"

export async function GET(request: NextRequest) {
  try {
    const searchParams = request.nextUrl.searchParams
    const userId = searchParams.get("userId")
    const page = searchParams.get("page") ? Number.parseInt(searchParams.get("page")!) : 1
    const limit = searchParams.get("limit") ? Number.parseInt(searchParams.get("limit")!) : 20

    // 模拟数据
    const playlists = Array.from({ length: 18 }, (_, i) => ({
      id: i + 1,
      name: `Playlist ${i + 1}`,
      description: `Description for playlist ${i + 1}`,
      cover: `/placeholder.svg?height=300&width=300&text=Playlist+${i + 1}`,
      songCount: 5 + i,
      createdBy: userId || "current-user",
      isPublic: true,
      createdAt: `2023-0${(i % 9) + 1}-0${(i % 9) + 1}T12:00:00Z`,
      updatedAt: `2023-0${(i % 9) + 1}-0${(i % 9) + 1}T12:00:00Z`,
    }))

    return NextResponse.json({
      data: playlists,
      pagination: {
        page,
        limit,
        total: 60,
        totalPages: Math.ceil(60 / limit),
      },
    })
  } catch (error) {
    console.error("Error fetching user playlists:", error)
    return NextResponse.json({ error: "Failed to fetch user playlists" }, { status: 500 })
  }
}

export async function POST(request: NextRequest) {
  try {
    const body = await request.json()
    const { playlistId } = body

    return NextResponse.json({
      success: true,
      message: `Playlist ${playlistId} added to favorites`,
    })
  } catch (error) {
    console.error("Error adding playlist to favorites:", error)
    return NextResponse.json({ error: "Failed to add playlist to favorites" }, { status: 500 })
  }
}

export async function DELETE(request: NextRequest) {
  try {
    const body = await request.json()
    const { playlistId } = body

    return NextResponse.json({
      success: true,
      message: `Playlist ${playlistId} removed from favorites`,
    })
  } catch (error) {
    console.error("Error removing playlist from favorites:", error)
    return NextResponse.json({ error: "Failed to remove playlist from favorites" }, { status: 500 })
  }
}

